package com.gaga.crm;


import com.gaga.crm.pojo.SysRole;
import com.gaga.crm.pojo.SysUser;
import com.gaga.crm.repository.UserRepository;
import com.gaga.crm.service.UserService;
import com.gaga.crm.vo.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class UserRepositoryTester {
    @Resource
    private UserRepository userRepository;

    @Resource
    private UserService userService;

    /**
     * 测试新增
     */
    /*@Test
    public void testInsert(){
        // save()方法保存对象，不带ID属性表示INSTER，带上ID属性表示UPDATE
        SysUser sysUser=new SysUser();
        sysUser.setName("JPA");
        sysUser.setRoleId(1L);
        sysUser.setPassword("123456");
        sysUser.setFlag(1);
        userRepository.save(sysUser);
    }*/

    /**
     * 主键id查询
     */
    @Test
    public void testGet(){
        SysUser sysUser=userRepository.findById(2L).get();
        System.out.println("userName："+sysUser.getName());
    }

    /**
     * 用户名查询
     */
    @Test
    public void testFindByName(){
        List<SysUser> list=userRepository.findByName("test");
        if (list!=null){
            for (SysUser sysUser : list) {
                System.out.println(sysUser.toString());
            }
        }
    }

    /**
     * 用户名模糊查询
     */
    @Test
    public void testFindByUserNameLike(){   //测试按用户名模糊查询
        List<SysUser> list=userRepository.findByNameLike("%小%");

    }

    /**
     * 通过name统计数量
     */
    @Test
    public void testCountByName(){   //测试按用户名模糊查询
        int rows=userRepository.countByName("小");
        System.out.println(rows);
    }

    /**
     * roleId查询
     */
    @Test
    public void testFindByRoleId(){
        List<SysUser> list=userRepository.findByRoleId(2L);
        if (list!=null){
            for (SysUser sysUser : list) {
                System.out.println("userName："+sysUser.getName());
            }
        }
    }

    /**
     * name查询
     */
    @Test
    public void testFindUsersByName(){
        List<SysUser> list=userRepository.findUsersByName("JPA");
        if (list!=null){
            for (SysUser sysUser : list) {
                System.out.println(sysUser.toString());
            }
        }
    }

    /**
     * 测试分页查询
     */
    @Test
    public void testFindPageByRoleId() {
        int page = 0, size = 2; // 分页要素，页数从 0 开始
        // 控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "id"); // Sort sort = new Sort(Sort.Direction.DESC, "usrId");
        // 控制分页的辅助类，可以设置页码、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<SysUser> userPage = userRepository.findPageByRoleId(2L,pageable);

        System.out.println("总记录数： " + userPage.getTotalElements());
        System.out.println("总页数： " + userPage.getTotalPages());
        System.out.println("当前页数： " + (userPage.getNumber() + 1));
        System.out.println("每页记录数： " + userPage.getSize());
        System.out.println("当前页记录数： " + userPage.getNumberOfElements());
        for (SysUser user : userPage.getContent()) {
            System.out.println(user.toString());
        }
    }

    /**
     * 测试分页查询（通过JpaSpecificationExecutor接口）
     */
    @Test
    public void testFindPageByMap() {
        int page = 0, size = 2; // 分页要素，页数从 0 开始
        // 控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "id"); // Sort sort = new Sort(Sort.Direction.DESC, "usrId");
        // 控制分页的辅助类，可以设置页码、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);

        Map param = new HashMap();
        param.put("roleId", 2L); // 测试一个查询条件，也可不给或给多个
        Page<SysUser> userPage = userService.findPageByMap(param, pageable);

        System.out.println("总记录数： " + userPage.getTotalElements());
        System.out.println("总页数： " + userPage.getTotalPages());
        System.out.println("当前页数： " + (userPage.getNumber() + 1));
        System.out.println("每页记录数： " + userPage.getSize());
        System.out.println("当前页记录数： " + userPage.getNumberOfElements());
        for (SysUser user : userPage.getContent()) {
            System.out.println(user.toString());
        }
    }

    /**
     * 测试多表联接查询
     */
    /*@Test
    public void testGetUserInfo(){
        UserInfo userInfo=userRepository.getUserInfo(2L);
        System.out.println("name:"+userInfo.getName());
        System.out.println("roleName:"+userInfo.getRoleName());
    }*/

    /**
     * 测试单向多对一关联
     */
    @Test
    public void testGetSysUser(){
        SysUser sysUser=userRepository.findById(2L).get();
        System.out.println("name:"+sysUser.getName());
        System.out.println("roleName:"+sysUser.getRole().getRole_name());
    }
}
